﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Runtime.Services;
using System.ComponentModel;
using Galaktika.BI.Runtime;
using Galaktika.BI.Silverlight.Services;

namespace Galaktika.BI.Silverlight.Controls
{
    public partial class Browser : UserControl, IBindableObject, IEditableObject
    {
        public Browser()
        {
            InitializeComponent();
            //base.LayoutUpdated += new EventHandler(Browser_LayoutUpdated);
        }

        //void Browser_LayoutUpdated(object sender, EventArgs e)
        //{
        //if (this.HtmlElement != null)
        //{
        //this.HtmlElement.SetStyleAttribute("display", "visible");
        //this.HtmlElement.SetStyleAttribute("width", string.Format("{0}px", Convert.ToInt32(this.ActualWidth)));
        //this.HtmlElement.SetStyleAttribute("height", string.Format("{0}px", Convert.ToInt32(this.ActualHeight)));
        //}
        //}

        public string AHtmlText { get; set; }

        public string AUrl { get; set; }

        public bool AShowUrl { get; set; }

        public virtual void BeginEdit()
        {
        }

        public virtual void CancelEdit()
        {
        }

        public virtual void EndEdit()
        {
            IExecutionContext exe = (IExecutionContext)this.Context.Services.GetService(typeof(IExecutionContext));
            if (exe != null)
            {
                if (this.AShowUrl)
                {
                    this.htmlHostURL.Visibility = this.htmlHostURLButton.Visibility = Visibility.Visible;
                    this.htmlHostURL.Text = this.AUrl;
                }
                else
                    this.htmlHostURLButton.Visibility = this.htmlHostURL.Visibility = Visibility.Collapsed;
               
                if (string.IsNullOrEmpty(this.AHtmlText))
                {
                    if (ScriptEngine.ContainsExpression(this.AUrl))
                    {                        
                        IUserTaskManager taskMgr = (IUserTaskManager)this.Context.Services.GetService(typeof(IUserTaskManager));
                        if (taskMgr != null)
                        {
                            taskMgr.RunTaskAsync(
                                string.Format(Galaktika.BI.Silverlight.Localization.AppTask_ProcessingUrlForControl, base.Name),
                                state =>
                                {
                                    var parsedUrl = exe.Parse(this.Context, this.AUrl);
                                    taskMgr.Dispatcher.BeginInvoke(() => htmlHost.ShowHtmlByUri(new Uri(parsedUrl)));
                                });
                        }
                    }
                    else
                    {
                        htmlHost.ShowHtmlByUri(new Uri(this.AUrl));
                    }
                }
                else
                {
                    if (ScriptEngine.ContainsExpression(this.AHtmlText))
                    {
                        IUserTaskManager taskMgr = (IUserTaskManager)this.Context.Services.GetService(typeof(IUserTaskManager));
                        if (taskMgr != null)
                        {
                            taskMgr.RunTaskAsync(
                                string.Format(Galaktika.BI.Silverlight.Localization.AppTask_ProcessingUrlForControl, base.Name),
                                state =>
                                {
                                    var parsedHtml = exe.Parse(this.Context, this.AHtmlText);
                                    taskMgr.Dispatcher.BeginInvoke(() => htmlHost.ShowHtmlBySource(parsedHtml));
                                });
                        }
                    }
                    else
                    {
                        htmlHost.ShowHtmlBySource(this.AHtmlText);
                        //base.HtmlElement.SetStyleAttribute("overflow", "auto");
                    }
                }
            }
        }

        public IBindingContext Context { get; set; }

        private IDictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }

        private void htmlHostURLButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IExecutionContext exe = (IExecutionContext)this.Context.Services.GetService(typeof(IExecutionContext));
                if (ScriptEngine.ContainsExpression(this.htmlHostURL.Text))
                {
                    IUserTaskManager taskMgr = (IUserTaskManager)this.Context.Services.GetService(typeof(IUserTaskManager));
                    if (taskMgr != null)
                    {
                        taskMgr.RunTaskAsync(
                            string.Format(Galaktika.BI.Silverlight.Localization.AppTask_ProcessingUrlForControl, base.Name),
                            state =>
                            {
                                var parsedUrl = exe.Parse(this.Context, this.htmlHostURL.Text);
                                taskMgr.Dispatcher.BeginInvoke(() => htmlHost.ShowHtmlByUri(new Uri(parsedUrl)));
                            });
                    }
                }
                else
                {
                    htmlHost.ShowHtmlByUri(new Uri(this.htmlHostURL.Text));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
